<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@include file="../includes/tag.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">    
    <title>业务信息管理平台</title>
    <script type="text/javascript" src="<%=basePath%>resources/lib/jquery/jquery.js"></script>
    <script type="text/javascript" src="<%=basePath%>resources/lib/webui/buss.ui.core.js"></script>
    <script type="text/javascript" src="<%=basePath%>resources/lib/webui/buss.ui.js"></script>
	<script type="text/javascript" src="<%=basePath%>resources/lib/jqgrid/js/i18n/grid.locale-cn.js"></script>
	<script type="text/javascript" src="<%=basePath%>resources/lib/jqgrid/js/jquery.jqGrid.min.js"></script>
	<script type="text/javascript" src="<%=basePath%>resources/lib/public.js"></script>
	
	<link rel="stylesheet" type="text/css" href="<%=basePath%>resources/lib/styles/themes/buss-ui.css">
	<link rel="stylesheet" type="text/css" href="<%=basePath%>resources/lib/jqgrid/css/jquery-ui.css">
	<link rel="stylesheet" type="text/css" href="<%=basePath%>resources/lib/jqgrid/css/ui.jqgrid.css">
    <link rel="stylesheet" type="text/css" href="<%=basePath%>resources/styles/public.css">
    <link rel="stylesheet" type="text/css" href="<%=basePath%>resources/styles/icon.css">
    
    <script type="text/javascript">
    	$(document).ready(function(){
    		
    		setGridData();
    		resize();
    		$(window).resize(resize);
    		
    		$("#search-btn").button({iconCls:'icon-search',handler:function(){
    			var jsonParam = syscore.formToJson($("form[name='_searchForm']"));
				$("#tabledata").jqGrid("setGridParam",{page:1,postData:jsonParam}).trigger("reloadGrid");
			}});
    		
    		$("#search-clear").button({iconCls:'icon-cancel',handler:function(){
    			$("form[name='_searchForm']").resetForm();
    			$("#search-btn").trigger("click");
    		}});
    		
    		$("#add-btn").button({iconCls:'icon-add',handler:function(){
    			editPropGroup("addpage","");
			}});
    		
			$("#edit-btn").button({iconCls:'icon-edit',handler:function(){
				var selRows = $("#tabledata").jqGrid('getGridParam',"selarrrow");
				if(selRows.length == 0){
					$.alert('提示','你没有选择要修改的属性分组信息!',300,140,'info');
					return;
				}
				if(selRows.length > 1){
					$.alert('提示','只能选择一条数据，请重新选择!',300,140,'info');
					return;
				}
				editPropGroup("editpage",selRows[0]);
			}});
			
			$("#del-btn").button({iconCls:'icon-remove',handler:function(){
				var selRows = $("#tabledata").jqGrid('getGridParam',"selarrrow");
				if(selRows.length == 0){
					$.alert('提示','你没有选择要修改删除的属性分组信息!',300,140,'info');
					return;
				}
				$.confirm('提示','确认要删除选中数据吗?',300,140,function(){
					var str = "";
					for(var i = 0;i < selRows.length;i++){
						if(i < selRows.length - 1){
							str += "ids=" + selRows[i] + "&";
						}else{
							str += "ids=" + selRows[i];
						}
					}
					$.post("<%=basePath%>admin/productprop/delete.do",str,function(rsData){
						if(rsData.message.code != '0'){
							$.alert('错误','删除属性分组信息失败!原因:' + rsData.message.mess,300,140,'error');
							return;
						}
						$("#tabledata").jqGrid({page:1}).trigger('reloadGrid');
					},"json");
				});	
			}});
			
			$("#propmanager-btn").button({iconCls:'icon-edit',handler:function(){
				var selRows = $("#tabledata").jqGrid('getGridParam',"selarrrow");
				if(selRows.length == 0){
					$.alert('提示','你没有选择要管理属性的分组信息!',300,140,'info');
					return;
				}
				if(selRows.length > 1){
					$.alert('提示','只能选择一条数据，请重新选择!',300,140,'info');
					return;
				}
				
				var panel = "<div>"+
					"<div class='propContext' style='padding:5px;'>" +
					"<table id='propContext_data'/>"+
					"<div id='propContext_datapage'/>"+
					"</div></div>";
				var dlg = $(panel).dialog({
					modal:true,
					height:438,
					width:700,
					title:'属性管理',
					iconCls:'icon',
					topTools:[
				          {text:'新增属性',type:'button',iconCls:'icon-add',plain:true,handler:function(){				        	 
				        	  editPropPage("addpage","",selRows[0]);
				          }},
				          {text:'修改属性',type:'button',iconCls:'icon-edit',plain:true,handler:function(){
				        	  var propRows = dlg.find("#propContext_data").jqGrid('getGridParam',"selarrrow");
								if(propRows.length == 0){
									$.alert('提示','你没有选择要修改属性信息!',300,140,'info');
									return;
								}
								if(propRows.length > 1){
									$.alert('提示','只能选择一条数据，请重新选择!',300,140,'info');
									return;
								}
								editPropPage("editpage",propRows[0],selRows[0]);
				          }},
				          {text:'删除属性',type:'button',iconCls:'icon-remove',plain:true,handler:function(){
				        	  var propRows = dlg.find("#propContext_data").jqGrid('getGridParam',"selarrrow");
								if(propRows.length == 0){
									$.alert('提示','你没有选择要修改删除的属性信息!',300,140,'info');
									return;
								}
								$.confirm('提示','确认要删除选中数据吗?',300,140,function(){
									var str = "";
									for(var i = 0;i < propRows.length;i++){
										if(i < propRows.length - 1){
											str += "ids=" + propRows[i] + "&";
										}else{
											str += "ids=" + propRows[i];
										}
									}
									$.post("<%=basePath%>admin/cateprop/delete.do",str,function(rsData){
										if(rsData.message.code != '0'){
											$.alert('错误','删除属性信息失败!原因:' + rsData.message.mess,300,140,'error');
											return;
										}
										dlg.find("#propContext_data").jqGrid().trigger('reloadGrid');
									},"json");
								});	
				          }},
				          {text:'关 闭',type:'button',iconCls:'icon-cancel',plain:true,handler:function(){
				        	  dlg.dialog("destroy");
				          }}
					 ]
				});
				
				var url = "<%=basePath%>admin/cateprop/list/" + selRows[0] + ".do";
				var colNames = ["属性编码","属性名称","选择类型","录入方式","查询显示","当前状态"];
				var colModel = [
				                {name:'code',index:'code',width:60,align:'left'},
				                {name:'name',index:'name',width:150,align:'left'},
				                {name:'selectType',index:'selectType',width:60,align:'center',formatter:function(cellValue){
				                	if(cellValue == "uniqueProp"){
				                		return "唯一";
				                	}else if(cellValue == "singleChoice"){
				                		return "单选";
				                	}else if(cellValue == "multichoice"){
				                		return "多选";
				                	}else{
				                		return "未知";
				                	}
				                }},
				                {name:'entryWay',index:'entryWay',width:60,align:'center',formatter:function(cellValue){
				                	if(cellValue == "manualWay"){
				                		return "手工录入";
				                	}else if(cellValue == "dicChoice"){
				                		return "字典选择";
				                	}else if(cellValue == "listChoice"){
				                		return "列表选择";
				                	}else{
				                		return "未知";
				                	}
				                }},
				                {name:'statisVisible',index:'statisVisible',width:60,align:'center',formatter:function(cellValue){
				                	if(cellValue){
				                		return "显示";
				                	}else{
				                		return "不显示";
				                	}
				                }},
				                {name:'status',index:'status',width:60,align:'center',formatter:syscore.formaterStatus}
				               ];
				dlg.find("#propContext_data").jqGrid({
					url:url,
					datatype:'json',
					height:317,
					multiselect: true,
					multiboxonly:true,
					autowidth:true,
					autoencode:true,
					viewrecords:true,
					forceFit:true,
					gridview:true,
					loadui:'block',
					rowNum:20,
					sortorder:'asc',
					rownumbers:true,
					colNames:colNames,
					colModel:colModel,
					jsonReader:{
						id:'id',
						root:'result.data',
						page:'result.page.curPageNo',
						total:'result.page.totalPage',
						repeatitems:false
					},
					prmNames:{
						page:'curPageNo',
						rows:'pageSize',
						sort:'orderField',
						order:'orderType'					
					},
					page:1,
					pagerpos:'left',
					pager:dlg.find("#propContext_datapage")
				});
			}});
			
    	});
			
    	/**编辑属性页面*/
    	function editPropPage(type,id,groupId){
    		
    		var url = "<%=basePath%>admin/cateprop/";
    		var pageUrl = "";
    		var saveUrl = "";
    		var title = "新增属性信息";
    		if(type == "editpage"){
    			title = "修改属性信息";
    			saveUrl = url + "edit.do";
    			pageUrl = url + "editpage.do?id=" + id;
    		}else{
    			saveUrl = url + "add.do";
    			pageUrl = url + "addpage.do?id=" + id + "&groupId=" + groupId;
    		}
    		var propEditDlg = syscore.editDialog(title,pageUrl,"",600,418,saveUrl,function(rsData){
    			if(rsData.message.code != '0'){
					 $.alert('提示','保存数据失败!原因:' + rsData.message.mess,300,140,'error');
					 return false;
				 }else{						 			
   			 		 $("#propContext_data").jqGrid().trigger('reloadGrid');			        			 	
   			 	     propEditDlg.dialog("destroy");
				 }
    		},function(){
    			propEditDlg.find("#sel_dicCode").button({iconCls:'icon-search',handler:function(){
    				selectDicData(propEditDlg);
    			}});
    			setControllerVisible(propEditDlg,propEditDlg.find("input[name=entryWay][checked]").val());
    			propEditDlg.find("input[name=entryWay]").change(function(){
    				setControllerVisible(propEditDlg,$(this).val());
    			});
    		});
    	}
    	
    	function selectDicData(dialog){
    		//alert(dialog);
    		var panel = "<div>"+
			"<div class='dicContext' style='padding:5px;'>" +
			"<table id='dicContext_data'/>"+
			"<div id='dicContext_datapage'/>"+
			"</div></div>";
    		var selDialog = $(panel).dialog({
    			modal:true,
    			header:true,
    			height:340,
    			width:550,
    			title:'选择数据字典',
    			iconCls:'icon',
    			buttons:[
    			         {text:'选 择',type:'button',iconCls:'icon-save',handler:function(){
    			        	 var rowId = selDialog.find("#dicContext_data").jqGrid("getGridParam","selrow");
    			        	 var rowObj = selDialog.find("#dicContext_data").jqGrid("getRowData",rowId);
    			        	 setDicDataVal(dialog,$(rowObj.code).html(),$(rowObj.name).html());
    			        	 selDialog.dialog("destroy");
    			         }},
    			         {text:'取 消',type:'button',iconCls:'icon-cancel',handler:function(){
    			        	 selDialog.dialog("destroy");
    			         }}
    			         ]
    		});
    		
			var url = "<%=basePath%>admin/dicdata/list/0.do";
    		var colNames = ["id","字典编码(Key)","字典名称(Value)","说明"];
    		var colModel = [
    		                {name:'id',index:'id',width:60,hidden:true},
    		                {name:'code',index:'code',width:100,align:'left',formatter:function(cellVal,opt,rowObj){
    		                	return "<a  style='text-decoration: underline;' href='javascript:void(0);' dicCode='"+rowObj.code+"' dicName='"+rowObj.name+"' class='selDicData'>"+cellVal+"</a>";
    		                }},
    		                {name:'name',index:'name',width:150,align:'left',formatter:function(cellVal,opt,rowObj){
    		                	return "<a  style='text-decoration: underline;' href='javascript:void(0);' dicCode='"+rowObj.code+"' dicName='"+rowObj.name+"' class='selDicData'>"+cellVal+"</a>";
    		                }},
    		                {name:'intro',index:'intro',width:300,align:'left'}
    		               ];
    		syscore.setGridData(selDialog.find("#dicContext_data"),selDialog.find("#dicContext_datapage"),url,colNames,colModel,false,0,'id',function(){
    			selDialog.find(".selDicData").click(function(){
    				setDicDataVal(dialog,$(this).attr("dicCode"),$(this).attr("dicName"));
    				selDialog.dialog("destroy");
    			});
    		});
    		
    		
		}	
    	
    	function setDicDataVal(dialog,code,name){
    		dialog.find("input[name='dicCode']").val(code);
    		dialog.find("input[name='dicName']").val(name);
    	}
    	
    	function setControllerVisible(dialog,val){
    		if(val == "manualWay"){
    			dialog.find("#prop_dicCode").hide();
    			dialog.find("#prop_listValues").hide();
			}else if(val  == "dicChoice"){
				dialog.find("#prop_dicCode").show();
				dialog.find("#prop_listValues").hide();
			}else if(val == "listChoice"){
				dialog.find("#prop_dicCode").hide();
				dialog.find("#prop_listValues").show();
			}
    	}
    	/**编辑属性分组信息页面*/
    	function editPropGroup(type,groupId){
    		var url = "<%=basePath %>admin/productprop/";
    		var pageUrl = "";
    		var saveUrl = "";
    		var title = "";
    		if(type == "editpage"){
    			pageUrl = url + "editpage.do?id=" + groupId;    			
    			title = "修改属性分组信息"
    		}else{
    			pageUrl += url + "addpage.do";    			
    			title = "新增属性分组信息";
    		}
    		var dlg = $("<div/>").dialog({
    			modal:true,
    			header:true,
    			height:300,
    			width:500,
    			title:title,
    			iconCls:'icon',
    			loadUrl:pageUrl,
    			buttons:[
    			         {text:"确  定",type:'button',iconCls:'icon-save',handler:function(){
    			        	 if(type == "editpage"){
    			        		 saveUrl = url + "edit.do";
    			        	 }else{
    			        		 saveUrl = url + "add.do";    			        		 
    			        	 }
    			        	 dlg.find("form").attr("action",saveUrl);
    			        	 syscore.ajaxSubmit(dlg.find("form"),function(rsData){
    			        		 if(rsData.message.code != '0'){
    								 $.alert('提示','保存数据失败!原因:' + rsData.message.mess,300,140,'error');
    								 return false;
    							 }else{						 			
    		        			 	 $("#tabledata").jqGrid().trigger('reloadGrid');			        			 	
    			       			 	 dlg.dialog("destroy");
    							 }	
    			        	 });
    			         }},
    			         {text:'关  闭',type:'button',iconCls:'icon-cancel',handler:function(){
    			        	 dlg.dialog("destroy");
    			         }}
    			        ],
    			onLoad:function(){
					dlg.find("input[name=name]").focusout(function(){
						if(dlg.find("input[name=displayName]").val() == ""){
							dlg.find("input[name=displayName]").val($(this).val());
						}
					});
    			}
    		});
    		dlg.dialog("loadPage");
    	};
    	
    	function setGridData(){
    		var url = "<%=basePath%>admin/productprop/list.do";
    		var colNames = ["属性分组名称","分组显示名称","当前状态","属性分组描述"];
    		var colModel = [
    		                {name:'name',index:'name',width:200,align:'left'},
    		                {name:'displayName',index:'displayName',width:200,align:'left'},
    		                {name:'status',index:'status',width:60,align:'center',formatter:syscore.formaterStatus},
    		                {name:'intro',index:'intro',width:300,align:'left'}
    		               ];
    		syscore.setGridData("#tabledata","#tablepage",url,colNames,colModel,true,0,'id');
    	};
    	
    	function resize(){
    		$("#p_container").width($(window).width());
			var h = $(window).height() - $(".nav_curloc").outerHeight() - $(".nav_search").outerHeight() - $(".nav_btn_bar").outerHeight() - 10;
			$(".context").height(h);
			$("#tabledata").jqGrid("setGridHeight",h - 50);
			$("#tabledata").jqGrid("setGridWidth",$(window).width() - 12);
		}
    </script>
</head>
<body>
  	<div id="p_container" style="height:100%;position:relative;">
  		<div class="nav_curloc">
	    	<label>您的当前位置：<bstag:navbar menuId="${menuid}"/></label>
	    </div>
	    <div class="nav_search" style="height: 28px;padding-top: 3px;">
	   		<form action="" name="_searchForm">
		    	<table height="100%" cellpadding="0" cellspacing="0">
		    		<tr>
		    			<td valign="middle">属性分组名称：</td>
		    			<td valign="middle">
		    				<input type="text" size="25" maxlength="100" class="text" name="name"/>
		    			</td>
		    			<td style="padding-left:5px;">
		    				<a id="search-btn" href="javascript:void(0);">查 询</a>
		    			</td>
		    			<td style="padding-left:5px;">
		    				<a id="search-clear" href="javascript:void(0);">清 空</a>
		    			</td>
		    		</tr>
		    	</table>
	    	</form>
	    </div>
	    <div class="nav_btn_bar">
	    	<table height="100%" cellpadding="0" cellspacing="0">
	    		<tr>
	    			<td valign="middle">
	    				<a id="add-btn" href="javascript:void(0);">新 增</a>
	    			</td>
	    			<td valign="middle"" style="padding-left:5px;">
	    				<a id="edit-btn" href="javascript:void(0);">修 改</a>
	    			</td>
	    			<td valign="middle" style="padding-left:5px;">
	    				<a id="del-btn" href="javascript:void(0);">删 除</a>
	    			</td>
	    			<td valign="middle" style="padding-left:5px;">
	    				<a id="propmanager-btn" href="javascript:void(0);">属性管理</a>
	    			</td>
	    		</tr>
	    	</table>
	    </div>
	    <div class="context">
	    	<table id="tabledata"></table>
	    	<div id="tablepage"></div>
	    </div>
  	</div> 
    
  </body>
</html>